<?php
namespace App\Console\Commands;
use App\Common\Logs\Logs;
use App\Models\Websites;
use App\Models\WebsitesCopy;
use App\Models\WebsitesTopic;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class WebsiteQuick extends Command
{
    /**
     * CreateArticle {param1?} {param2?}
     * The name and signature of the console command.
     * @var string
     */
    protected $signature = 'WebsiteQuick';


    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '网站的快速连接';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        //获取网站信息
        $websiteData = Websites::query()->where("is_quick", 0)->select("id", "names", "domains", "is_collect")->first();
        if (!$websiteData) {
            Logs::crontabLog("暂无网站需要快速连接", [], "website_quick");
            return false;
        }
        $websiteId = $websiteData->id;
        $websiteDomain = $websiteData->domains;
        $isCollect = $websiteData->is_collect;
        //初始化数据
        $addData = [];
        $initData = [];
        $initData[0]['website_id'] = $websiteId;
        $initData[0]['names'] = "品达发稿";
        $initData[0]['jump_url'] = "https://fagao.pindarpr.com/";
        $initData[0]['sorts'] = 1;
        $initData[0]['created_at'] = date("Y-m-d H:i:s");
        $addData[]=$websiteId;
        //主域名集合
        $otherWebsite=array(
            'rwfagao.com','kylingc.com','5trw.com','58rwl.com','rwtui.com','fagao888.com','51fgba.com','rwfgb.com','5fg8.com'
        );
        if($isCollect == 1){//该网站已收录
            $urlArr=explode('.',$websiteDomain);
            $newDomain =  $urlArr[count($urlArr)-2].".".$urlArr[count($urlArr)-1];
            //先取该网址主域名下的5个未收录的网站
            $noShouluArr = Websites::query()
                ->where("is_collect",0)
                ->select("id","names","domains","is_collect")
                ->inRandomOrder()
                ->limit(5)
                ->where("domains","like","%{$newDomain}%")
                ->get()
                ->toArray();
            if($noShouluArr){
                foreach ($noShouluArr as $kk=>$noShoulu){
                    $addData[]=$noShoulu['id'];
                    $initData[$kk+1]['website_id'] = $websiteId;
                    $initData[$kk+1]['names'] = $noShoulu['names'];
                    $initData[$kk+1]['jump_url'] = "http://".$noShoulu['domains'];
                    $initData[$kk+1]['sorts'] = $kk+2;
                    $initData[$kk+1]['created_at'] = date("Y-m-d H:i:s");
                }
            }
            $i=0;
            //排除当前的主域名
            $newOtherWebsite=array();
            foreach ($otherWebsite as $others){
                if($others != $newDomain){
                    $newOtherWebsite[$i]['domains'] = $others;
                    $i++;
                }
            }
            $initCount = count($initData);
            $j = $initCount-1;
            //其他的域名下未收录的网站各一个
            foreach ($newOtherWebsite as $newKey=>$newVal){
                $otherArr = Websites::query()
                    ->where("is_collect",0)
                    ->select("id","names","domains","is_collect")
                    ->inRandomOrder()
                    ->limit(1)
                    ->where("domains","like","%{$newVal['domains']}%")
                    ->first();
                if($otherArr){
                    $addData[]=$otherArr['id'];
                    $initData[$j+1]['website_id'] = $websiteId;
                    $initData[$j+1]['names'] = $otherArr['names'];
                    $initData[$j+1]['jump_url'] = "http://".$otherArr['domains'];
                    $initData[$j+1]['sorts'] = $j+2;
                    $initData[$j+1]['created_at'] = date("Y-m-d H:i:s");
                    $j++;
                }
            }
        }else{
            //获取已收录的网站3个
            $collectData = Websites::query()
                ->where("is_collect",1)
                ->inRandomOrder()
                ->limit(3)
                ->select("id","names","domains","is_collect")
                ->get()
                ->toArray();
            if($collectData){
                foreach ($collectData as $kkk=>$collects){
                    $addData[]=$collects['id'];
                    $initData[$kkk+1]['website_id'] = $websiteId;
                    $initData[$kkk+1]['names'] = $collects['names'];
                    $initData[$kkk+1]['jump_url'] = "http://".$collects['domains'];
                    $initData[$kkk+1]['sorts'] = $kkk+2;
                    $initData[$kkk+1]['created_at'] = date("Y-m-d H:i:s");
                }
            }
            //其他的域名的网站各一个
            $initCount = count($initData);
            $m = $initCount-1;
            foreach ($otherWebsite as $otherWebsiteVal){
                $otherArr2 = Websites::query()
                    ->where("is_collect",0)
                    ->where("id","<>",$websiteId)
                    ->select("id","names","domains","is_collect")
                    ->inRandomOrder()
                    ->limit(1)
                    ->where("domains","like","%{$otherWebsiteVal}%")
                    ->first();
                if($otherArr2){
                    $addData[]=$otherArr2['id'];
                    $initData[$m+1]['website_id'] = $websiteId;
                    $initData[$m+1]['names'] = $otherArr2['names'];
                    $initData[$m+1]['jump_url'] = "http://".$otherArr2['domains'];
                    $initData[$m+1]['sorts'] = $m+2;
                    $initData[$m+1]['created_at'] = date("Y-m-d H:i:s");
                    $m++;
                }
            }
        }
        //确定数组数量，剩余的随机
        $endCount = count($initData);
        $syCount  = 20 - $endCount;
        $k = $endCount - 1;
        if($syCount > 0){
            $supplementData = Websites::query()
                ->where("is_collect",0)
                ->whereNotIn("id",$addData)
                ->select("id","names","domains","is_collect")
                ->inRandomOrder()
                ->limit($syCount)
                ->get()
                ->toArray();
            foreach ($supplementData as $key=>$supplement){
                $initData[$k+1]['website_id'] = $websiteId;
                $initData[$k+1]['names'] = $supplement['names'];
                $initData[$k+1]['jump_url'] = "http://".$supplement['domains'];
                $initData[$k+1]['sorts'] = $k+2;
                $initData[$k+1]['created_at'] = date("Y-m-d H:i:s");
                $k++;
            }
        }
        DB::table("pd_website_quick")->insert($initData);
        Websites::query()->where("id",$websiteId)->update(['is_quick'=>1]);
        echo "网站id:".$websiteId.",添加快速连接成功".PHP_EOL;
    }


}
